package com.totoro.tymapping.executor.condition.impl;

import com.totoro.tymapping.constants.ConditionConst;
import com.totoro.tymapping.constants.StaticSystemVarConst;
import com.totoro.tymapping.executor.condition.ConditionExecutor;
import com.totoro.tymapping.utils.JexlUtil;
import com.totoro.tymapping.utils.PapiUtil;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/13 - 15:41
 * @description
 **/
public class PapiExecutor implements ConditionExecutor {
    @Override
    public Boolean execute(String condition, Player player, ItemStack matchItem) {
        String finalCondition = getFinalCondition(condition, ConditionConst.PAPI);
        try {
            // 判断是否安装了PAPI变量依赖，如果安装了，则执行PAPI变量解析
            if(StaticSystemVarConst.dependentPlaceholderAPIEnable) {
                finalCondition = PapiUtil.parseStrPapiVariable(player, finalCondition);
            }
            // 执行表达式
            return (Boolean) JexlUtil.evaluateExpressions(finalCondition);
        } catch (Exception e) {
            return false;
        }
    }
}
